function mov_error( fileName )
%--------------------------------------------------------------------------
% Moving Spectral Average Error Comparison
%
% Cooper Baker - 2014
%--------------------------------------------------------------------------

close all;

name = 'Moving Spectral Average Error';

% set path
p = genpath( '../../analyze' );
addpath( p );

% get audio file info
fileInfo = audioinfo( [ fileName, '.classic.wav' ] );
sr       = fileInfo.SampleRate;
samps    = fileInfo.TotalSamples;
winSize  = 512;
overlap  = 4;

% make file names
rawFile  = sprintf( '%s.classic.mov.err.csv' , fileName );
lockFile = sprintf( '%s.lock.mov.err.csv', fileName );
peakFile = sprintf( '%s.peak.mov.err.csv', fileName );
bankFile = sprintf( '%s.bank.mov.err.csv', fileName );
solaFile = sprintf( '%s.sola.mov.err.csv', fileName );
olaFile  = sprintf( '%s.ola.mov.err.csv' , fileName );

% load file data
rawData  = csvread( rawFile  );
lockData = csvread( lockFile );
peakData = csvread( peakFile );
bankData = csvread( bankFile );
solaData = csvread( solaFile );
olaData  = csvread( olaFile  );

% get data size
size = length( rawData );

% calculate msec grid values
msec     = ( samps / sr ) * 1000;
gridMsec = 500;
gridHops = gridMsec / ( ( ( winSize / overlap ) / sr ) * 1000 );
hopSize  = winSize / overlap;
hopMsec  = ( hopSize / sr ) * 1000;

% calculate data maximums
rawMax  = max( rawData  );
lockMax = max( lockData );
peakMax = max( peakData );
bankMax = max( bankData );
solaMax = max( solaData );
olaMax  = max( olaData  );

% calculate data averages
rawAvg  = sum( rawData  ) / size;
lockAvg = sum( lockData ) / size;
peakAvg = sum( peakData ) / size;
bankAvg = sum( bankData ) / size;
solaAvg = sum( solaData ) / size;
olaAvg  = sum( olaData  ) / size;

% find maximum maximum and minimum maximum
MaxMax = max( [ rawMax lockMax peakMax bankMax solaMax olaMax ] );
MinMax = min( [ rawMax lockMax peakMax bankMax solaMax olaMax ] );

% find maximum average and minimum average
MaxAvg = max( [ rawAvg lockAvg peakAvg bankAvg solaAvg olaAvg ] );
MinAvg = min( [ rawAvg lockAvg peakAvg bankAvg solaAvg olaAvg ] );

% graph formatting
fontName   = 'Times New Roman';
fontSize   = 12;
xTxt       = size * 0.01;
yTxt       = MaxMax * .9;

% set up plot window
fig = figure( 1 );
set( fig, 'Name', sprintf( '%s - %s.wav', name, fileName ) );
set( fig, 'Position', [ 0 0 800 1200 ] );
set( fig, 'defaultAxesFontName', fontName );
set( fig, 'defaultTextFontName', fontName );
set( fig, 'defaultlinelinewidth', 3 );
set( fig, 'defaultaxescolororder', [ 0.8 0.2 0.2 ] );

% plot fft ifft raw
title = 'Classic';
set( subplot( 1, 1, 1 ), 'Position', [ 0.1, 5.5/7, 0.8, 1/7.333 ] );
plot( rawData );
axis( [ 0 size 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ 0 : gridHops : size ] );
set( gca, 'XTickLabel', [] );
legStr = sprintf( ' %s - Avg: %2.5f Max: %2.5f ', title, rawAvg, rawMax );
text( xTxt, yTxt, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% plot fft ifft phase locked
title = 'Lock';
set( subplot( 6, 1, 2 ), 'Position', [ 0.1, 4.5/7, 0.8, 1/7.333 ] );
plot( lockData );
axis( [ 0 size 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ 0 : gridHops : size ] );
set( gca, 'XTickLabel', [] );
legStr = sprintf( ' %s - Avg: %2.5f Max: %2.5f ', title, lockAvg, lockMax );
text( xTxt, yTxt, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% plot fft ifft peak tracking
title = 'Peak';
set( subplot( 6, 1, 3 ), 'Position', [ 0.1, 3.5/7, 0.8, 1/7.333 ] );
plot( peakData );
axis( [ 0 size 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ 0 : gridHops : size ] );
set( gca, 'XTickLabel', [] );
legStr = sprintf( ' %s - Avg: %2.5f Max: %2.5f ', title, peakAvg, peakMax );
text( xTxt, yTxt, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% plot fft oscillator bank
title = 'Bank';
set( subplot( 6, 1, 4 ), 'Position', [ 0.1, 2.5/7, 0.8, 1/7.333 ] );
plot( bankData );
axis( [ 0 size 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ 0 : gridHops : size ] );
set( gca, 'XTickLabel', [] );
legStr = sprintf( ' %s - Avg: %2.5f Max: %2.5f ', title, bankAvg, bankMax );
text( xTxt, yTxt, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% plot synchronous overlap add
title = 'Sola';
set( subplot( 6, 1, 5 ), 'Position', [ 0.1, 1.5/7, 0.8, 1/7.333 ] );
plot( solaData );
axis( [ 0 size 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ 0 : gridHops : size ] );
set( gca, 'XTickLabel', [] );
legStr = sprintf( ' %s - Avg: %2.5f Max: %2.5f ', title, solaAvg, solaMax );
text( xTxt, yTxt, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% plot overlap add
title = 'Ola';
set( subplot( 6, 1, 6 ), 'Position', [ 0.1, 0.5/7, 0.8, 1/7.333 ] );
plot( olaData );
axis( [ 0 size 0 MaxMax ] );
grid on;
set( gca, 'XTick', [ 0 : gridHops : size ] );
set( gca, 'XTickLabel', [ 0 : gridMsec : msec ] );
xlabel( 'Milliseconds' );
ylabel( 'Sones' );
legStr = sprintf( ' %s - Avg: %2.5f Max: %2.5f ', title, olaAvg, olaMax );
text( xTxt, yTxt, legStr, 'BackgroundColor', 'w', 'EdgeColor', 'k' );

% tighten up borders
tightfig();

% move y label to middle of graphs
ylabel( 'Sones', 'Position', [ -size / 23 ( ( MaxMax * 6.1666 ) / 2 ) ] );

% write plot to file
hgexport( fig, [ fileName, '.err.mov.eps' ] );

% EOF
